package cn.z2huo.knowledge.collection.map.hashmap.source;

/**
 * <p>
 *
 * @author z2huo
 */
public class GetNodeTest {

    public static void main(String[] args) {
        String key = "name";
        int keyHashCode = key.hashCode();
        System.out.println(keyHashCode);
        System.out.println(Integer.toBinaryString(keyHashCode));
        System.out.println(keyHashCode >>> 16);
        System.out.println(Integer.toBinaryString(keyHashCode >>> 16));

        System.out.println();
        System.out.println(hash(key));
        System.out.println(String.format("%32s", Integer.toBinaryString(hash(key))).replace(' ', '0'));
        int length = 16;
        System.out.println(String.format("%32s", Integer.toBinaryString(length - 1)).replace(' ', '0'));
        int a = (16 - 1) & hash(key);
        System.out.println(a);
        System.out.println(String.format("%32s", Integer.toBinaryString(a)).replace(' ', '0'));

        System.out.println();
        length = 16;
        System.out.println(binaryString32(length - 1));
        for (int i = 0; i < 32; i++) {
            System.out.println(i + " " + ((length - 1) & hash(i)) + " " + binaryString32((length - 1) & hash(i)));
        }
    }

    static int hash(Object key) {
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }

    static String binaryString32(int a) {
        return String.format("%32s", Integer.toBinaryString(a)).replace(' ', '0');
    }

}
